package csu.edu.flatprogram.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import csu.edu.flatprogram.common.CommonResponse;
import csu.edu.flatprogram.entity.Item;
import csu.edu.flatprogram.entity.LineItem;
import csu.edu.flatprogram.entity.Order;
import csu.edu.flatprogram.entity.OrderStatus;
import csu.edu.flatprogram.persistence.ItemMapper;
import csu.edu.flatprogram.persistence.LineItemMapper;
import csu.edu.flatprogram.persistence.OrderMapper;
import csu.edu.flatprogram.persistence.OrderStatusMapper;
import csu.edu.flatprogram.service.OrderService;
import csu.edu.flatprogram.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("orderService")
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    private OrderStatusMapper orderStatusMapper;
    @Autowired
    private LineItemMapper lineItemMapper;
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    OrderVO orderVO;

    @Override
    public CommonResponse<List<OrderVO>> getOrderByUserId(String userId){
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",userId);
        List<Order> orderList = orderMapper.selectList(queryWrapper);
        if (orderList.isEmpty()){
            return CommonResponse.createForError("用户无订单信息");
        }
        List<OrderVO> orderVOList = new ArrayList<>();
        for (Order order : orderList){
            OrderVO orderVO = orderToOrderVO(order);
            orderVOList.add(orderVO);
        }
        return CommonResponse.createForSuccess(orderVOList);
    }
        @Override
    public CommonResponse<OrderVO> getOrdersByOrderId(String orderId){
        Order order = orderMapper.selectById(orderId);
        if (order==null){
            return CommonResponse.createForError("没有该订单号的订单信息");
        }
        OrderVO orderVO = orderToOrderVO(order);
        return CommonResponse.createForSuccess(orderVO);
    }
    private OrderVO orderToOrderVO(Order order) {
        OrderVO orderVO = new OrderVO();
        orderVO.setOrderId(order.getOrderId());
        orderVO.setUserId(order.getUserid());
        orderVO.setOrderDate(order.getOrderDate());
        orderVO.setBillAddress1(order.getBillAddress1());
        orderVO.setBillAddress2(order.getBillAddress2());


        OrderStatus orderStatus = orderStatusMapper.selectById(order.getOrderId());
        orderVO.setStatus(orderStatus.getStatus());

        QueryWrapper<LineItem> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("orderid",order.getOrderId());
        List<LineItem> lineItems = lineItemMapper.selectList(queryWrapper);

        return orderVO;
    }

    @Override
    public CommonResponse<OrderVO> addOrder(OrderVO orderVO){
        Order order = new Order();
        order.setOrderId(orderVO.getOrderId());
        order.setUserid(orderVO.getUserId());
        order.setOrderDate(orderVO.getOrderDate());
        order.setBillAddress1(orderVO.getBillAddress1());
        order.setBillAddress2(orderVO.getBillAddress2());



        if (orderMapper.selectById(order.getOrderId())==null) {
            //插入OrderStatus中的数据
            int insertOrder = orderMapper.insert(order);

            OrderStatus orderStatus = new OrderStatus();
            orderStatus.setStatus(orderVO.getStatus());
            orderStatus.setOrderId(orderVO.getOrderId());
            orderStatus.setLinenum(orderVO.getLineNumber());
            orderStatus.setTimestamp(orderVO.getOrderDate());
            int insertOrderStatus = orderStatusMapper.insert(orderStatus);

        }

        //插入LineItem中的数据

            LineItem lineItem = new LineItem();
            lineItem.setOrderId(orderVO.getOrderId());
            lineItem.setLineNumber(orderVO.getLineNumber());
            lineItem.setItemId(orderVO.getItemId());
            lineItem.setQuantity(orderVO.getQuantity());
            Item item = itemMapper.selectById(lineItem.getItemId());
            lineItem.setUnitPrice(orderVO.getUnitPrice());
            int insertLineItem = lineItemMapper.insert(lineItem);



        if(insertLineItem==1) {
            return CommonResponse.createForSuccess("创建订单成功",orderVO);
        }else {
            return CommonResponse.createForError("创建订单失败");
        }
    }

    public CommonResponse<List<OrderVO>> searchOrderList(String keyword){
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.like("orderid",keyword);
        List<Order> orders = orderMapper.selectList(orderQueryWrapper);
        List<OrderVO> orderVOS = new ArrayList<>();
        for (Order order:orders){
            OrderVO orderVO = orderToOrderVO(order);
            orderVOS.add(orderVO);
        }
        return CommonResponse.createForSuccess(orderVOS);
    }

    //删除订单delete order by orderID
    @Override
    public CommonResponse<OrderVO> deleteOrder(OrderVO orderVO){
        System.out.println("****************************");

        System.out.println(orderVO.getOrderId());
        int rows = orderMapper.selectCount(Wrappers.<Order>query().eq("orderid",orderVO.getOrderId()));

        if(rows==0) {
            return CommonResponse.createForSuccess("无订单",orderVO);
        }else {
            orderMapper.delete(Wrappers.<Order>query().eq("orderid",orderVO.getOrderId()));
            lineItemMapper.delete(Wrappers.<LineItem>query().eq("orderid",orderVO.getOrderId()));
            orderStatusMapper.delete(Wrappers.<OrderStatus>query().eq("orderid",orderVO.getOrderId()));

            return CommonResponse.createForError("删除订单成功");
        }
    }
}
